<?php

namespace App\Http\Controllers\Backend\Bank;

use App\Http\Requests\Backend\Bank\ProgressBankRequest;
use App\Models\ProgressBank;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;

class ProgressBankController extends Controller
{
    protected $admin_user;

    public function __construct()
    {
        $this->middleware('auth:backend');
        $this->admin_user = Auth::guard('backend')->user();
    }

    /**
     * Description:办卡进度列表
     * Author: tanjun
     * DateTime: 2019-04-13 13:17
     * @param Request $request
     * @return mixed
     */
    public function index(Request $request)
    {
        $progress = new ProgressBank();
        $progress->setConnection('backend');

        $page_size = $request->get('page_size', null);

        if($page_size) {
            $list = $progress->orderByDesc('created_at')->paginate($page_size);
            if ($list) {
                $info = [];
                foreach ($list as $v) {
                    $info[] = [
                        'id' => $v->id,
                        "url" => $v->url,
                        "status" => $v->status,
                        "created_at" => $v->created_at->format('Y-m-d H:i:s'),
                        "updated_at" => $v->updated_at->format('Y-m-d H:i:s'),
                    ];
                }
                return success(['list' => $info, 'total' => $list->total()]);
            }
        } else {
            $list = $progress->orderByDesc('created_at')->get();
            return success($list);
        }

        return success([]);
    }


    /**
     * Description: 详情获取
     * Author: tanjun
     * DateTime: 2019-04-10 19:50
     * @param $id
     * @return mixed
     */
    public function show($id)
    {
        $id = intval($id);
        if ($id < 1) {
            return failed('没有找到数据');
        }

        $progress = new ProgressBank();
        $progress->setConnection('backend');

        $progressInfo = $progress->where('id', $id)->first();

        if ($progressInfo) {
            $progressInfo->url = getOssUrl($progressInfo->url);
            return success($progressInfo);
        }
        return failed('当前信息不存在');
    }

    /**
     * Description:更新
     * Author: tanjun
     * DateTime: 2019-04-13 13:19
     * @param ProgressBankRequest $request
     * @param $id
     * @return mixed
     */
    public function update(ProgressBankRequest $request, $id)
    {
        $id = intval($id);
        if ($id < 1) {
            return failed('没有找到需要更新的数据');
        }

        $params = $this->extractInputFromRules($request, $request->rules());
        $progress = new ProgressBank();
        $info = $progress->where('id', $id)->update($params);
        return $info ? updated('') : failed('更新失败');
    }

    /**
     * Description: 创建数据
     * Author: tanjun
     * DateTime: 2019-04-13 12:27
     * @param ProgressBankRequest $request
     * @return mixed
     */
    public function store(ProgressBankRequest $request)
    {
        $params = $this->extractInputFromRules($request, $request->rules());
        $progress = new ProgressBank();
        $progress->fill($params);
        $info = $progress->save($params);
        return $info ? stored('') : failed('添加失败');
    }

    /**
     * Description:删除数据
     * Author: tanjun
     * DateTime: 2019-04-13 13:11
     * @param $id
     * @return mixed
     * @throws \Exception
     */
    public function destroy($id)
    {
        $id = intval($id);
        if ($id < 1) {
            return failed('没有找到需要删除的数据');
        }
        $progress = new ProgressBank();
        $info = $progress->where('id', $id)->delete();
        return $info ? deleted() : failed('删除失败');
    }
}
